/*
 * Created on 2006-7-17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.powerunion.datacollection.report.excelreport.impl;

import com.powerunion.datacollection.report.excelreport.base.BaseBand;
import com.powerunion.datacollection.report.excelreport.base.BaseElement;
import com.powerunion.datacollection.report.excelreport.base.BaseReportWriter;
import com.powerunion.datacollection.report.excelreport.base.IElementWriter;
import com.powerunion.datacollection.report.excelreport.base.ITempletContainer;
import com.powerunion.datacollection.report.excelreport.base.Parameter;
import com.powerunion.datacollection.report.excelreport.base.element.ElementFactory;
import com.powerunion.datacollection.report.excelreport.util.AnalyseTempletTool;

import jxl.Cell;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;

/**
 * 卡片式报表生成器实现类，该类负责生成一个卡片式报表。
 * @author juny
 */
public class CardStyleReportWriterImpl extends BaseReportWriter{
    /* 
     * @see excel.report.util.IWriter#analysisTemplet(jxl.write.WritableSheet)
     */
    public void analysisTemplet(WritableSheet sheet)throws Exception {
        WritableCell wc = null;
        String content = null;
        IElementWriter element = null;
        ElementFactory factory = ElementFactory.getFactory();
        
        ITempletContainer container = this.getContainer();
        container.setBegin(0, 0);
        
        //分析模板文件
        int rows = sheet.getRows();
        for(int i=0; i<rows; i++){
            Cell[] cells = sheet.getRow(i);
            for(int j=0; j<cells.length; j++){
                wc = sheet.getWritableCell(cells[j].getColumn(),cells[j].getRow());
                content = wc.getContents();
                if(AnalyseTempletTool.isTempletElement(content) ){
                    element = factory.getElement(wc, container);
                    if(null != element){
                        //卡片式报表不允许定义band
                        if(!(element instanceof BaseBand)){
                            container.addElement((BaseElement)element, j, i);
                        }
                    }
                }
            }
        }
        
        container.setEnd(sheet.getColumns() - 1, rows - 1);
        
       
        //读取模板内合并单元格信息
        //getMergedCells(sheet);
    }
    
    /* 
     * @see excel.report.util.IWriter#writeData(excel.report.util.Parameter)
     */
    public void writeData(Parameter param) throws Exception {
        int templetEndRow = 0, templetBeginRow = 0;
        WritableCell wc = null;
        BaseElement element = null;
        WritableSheet sheet = param.sheet;
        ITempletContainer container = this.getContainer();
        
        //取得模板的开始行和结束行
        templetBeginRow = container.getBeginRow();
        templetEndRow = container.getEndRow(); 
        
        for (int i = templetBeginRow; i <= templetEndRow; i++) {
            Cell[] cells = sheet.getRow(i);
            for(int j=0; j<cells.length; j++){
                element = container.getElement(j, i);
                if(null != element){
                    wc = sheet.getWritableCell(j, i);
                    param.wc = wc;
                    element.write(param);
                    //判断是否是新产生的单元格
                    if(wc != param.wc){
                        sheet.addCell(param.wc);
                    }
                    param.wc = null;
                }
            }
        }
        //不用拷贝合并单元格
    }
}
